package oj;

import java.util.Stack;

public class EvalRPN {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String s : tokens){
            if(!isOperator(s)){
                int x = Integer.parseInt(s);
                stack.push(x);
            }else{
                int val2 = stack.pop();
                int val1 = stack.pop();
                switch (s){
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 /val2);
                        break;
                }
            }
        }
        return stack.peek();
    }
    private boolean isOperator(String s){
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
            return true;
        }
        return false;
    }
}
